package com.ruoyi.celoan.service.statistics.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.celoan.entity.DalianDataReportKeyEntity;
import com.ruoyi.celoan.entity.ExTradeEffectEnterprisesDetailSqsEntity;
import com.ruoyi.celoan.handanUtils.HanDanHttpSelectUtils;
import com.ruoyi.celoan.handanUtils.HanDanHttpUtils;
import com.ruoyi.celoan.handanUtils.response.CountryPlatformResponse;
import com.ruoyi.celoan.mapper.ExTradeEffectFinancialproductsDetailSqsMapper;
import com.ruoyi.celoan.service.IEcDalianDataReportKeyService;
import com.ruoyi.celoan.service.IEcDalianDataReportLogsService;
import com.ruoyi.celoan.service.statistics.ExTradeEffectEnterprisesDetailSqsService;
import com.ruoyi.celoan.util.ListUtils;
import com.ruoyi.common.utils.FreeImagesToPdf;
import com.ruoyi.manage.enterprise.domain.EcEnterpriseFile;
import com.ruoyi.manage.enterprise.domain.EcEnterpriseInformation;
import com.ruoyi.manage.enterprise.service.IEcEnterpriseFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class ExTradeEffectFinancialproductsDetailSqsServiceImpl extends ServiceImpl<ExTradeEffectFinancialproductsDetailSqsMapper, ExTradeEffectEnterprisesDetailSqsEntity> implements ExTradeEffectEnterprisesDetailSqsService {
    private static final Logger log = LoggerFactory.getLogger(ExTradeEffectFinancialproductsDetailSqsServiceImpl.class);
    @Autowired
    private IEcDalianDataReportLogsService dalianDataReportLogsService;
    private static String url = "/api/upload/financialProducts";
    private static String appKey = "CCB004";
    private static String app_secret = "ujh9rrcc59k6es64";
    private static String app_id = "iv63koa14o06lw74";
    private static boolean flag = true;
    @Autowired
    private IEcDalianDataReportKeyService keyService;

    public void getDataReport(String interfaceName, String areaCode) {
        String startDate = LocalDateTime.now().plusDays(-2L).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        QueryWrapper<ExTradeEffectEnterprisesDetailSqsEntity> queryWrapper = new QueryWrapper();
        queryWrapper.eq("area_code", areaCode);
        queryWrapper.eq("isflag", Integer.valueOf(0));
        log.info("开始时间：" + startDate);
        List<ExTradeEffectEnterprisesDetailSqsEntity> listEntity = ((ExTradeEffectFinancialproductsDetailSqsMapper) this.baseMapper).selectList((Wrapper) queryWrapper);
        if (CollUtil.isNotEmpty(listEntity)) {
            List<List<ExTradeEffectEnterprisesDetailSqsEntity>> retList = ListUtils.split(listEntity, 100);
            this.dalianDataReportLogsService.addLogs(interfaceName + "数据授权上传文件开始", listEntity.size() + "条");
            String date = LocalDate.now().toString();
            List<Long> updateList = new ArrayList<>();
            for (int i = 0; i < retList.size(); i++) {
                try {
                    List<ExTradeEffectEnterprisesDetailSqsEntity> reportList = retList.get(i);
                    List<Long> addlist = new ArrayList<>();
                    shouquan(reportList, updateList);
                } catch (Exception e) {
                    e.printStackTrace();
                    log.info("数据上报异常:" + e.getMessage());
                    this.dalianDataReportLogsService.addLogs(interfaceName + "数据授权上传文件结束", JSONObject.toJSONString(e.getMessage()));
                }
            }
            if (CollUtil.isNotEmpty(updateList)) {
                UpdateWrapper<ExTradeEffectEnterprisesDetailSqsEntity> updateWrapper = new UpdateWrapper();
                updateWrapper.in("id", updateList);
                updateWrapper.set("isflag", Integer.valueOf(1));
                update((Wrapper) updateWrapper);
                this.dalianDataReportLogsService.addLogs(interfaceName + "上报数据上报成功条数:", updateList.size() + "条");
            }
        }
    }

    @Autowired
    IEcEnterpriseFileService iEcEnterpriseFileService;

    @Override
    public void addShouquanshuList(EcEnterpriseInformation enterpriseInformation) {
//        生成授权书地址
        List<EcEnterpriseFile> list = iEcEnterpriseFileService.lambdaQuery().eq(EcEnterpriseFile::getEnterpriseId, enterpriseInformation.getId()).list();
        List<String> fileList = list.stream().map(m -> m.getFileUrl()).collect(Collectors.toList());
        String pdfUrl = FreeImagesToPdf.generatePdfByImg(fileList);

        ExTradeEffectEnterprisesDetailSqsEntity enterprisesDetailSqsEntity = new ExTradeEffectEnterprisesDetailSqsEntity();
        enterprisesDetailSqsEntity.setEnterpriseName(enterpriseInformation.getEnterpriseName());
        enterprisesDetailSqsEntity.setFileUrl(pdfUrl);
    }

    public void cancelLicense(String interfaceName, String areaCode) {
        log.info("加载调用客户端配置");
        String token = HanDanHttpUtils.getToken();
        DalianDataReportKeyEntity entity = this.keyService.selectEntity(appKey);
        List<String> list = new ArrayList<>(Arrays.asList(new String[]{"df81aec0463e46feb13e30e78190d195"}));
        list.forEach(licenseId -> {
            Map<String, String> map = new HashMap<>();
            map.put("enterpriseName", "??????????");
            map.put("uniscId", "91110108667518422G");
            map.put("platformId", "1306");
            map.put("licenseId", licenseId);
            try {
                HanDanHttpSelectUtils.cancelLicense(JSONObject.toJSONString(map), token);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    public void shouquan(List<ExTradeEffectEnterprisesDetailSqsEntity> reportList, List<Long> updateList) {
        String token = HanDanHttpUtils.getToken();
        reportList.stream().forEach(dto -> {
            log.info("加载调用客户端配置");
            DalianDataReportKeyEntity entity = this.keyService.selectEntity(appKey);
            log.info("调用文件信息：" + JSONObject.toJSONString(dto));
            log.info("上传授权接口：/api/creditInquiry/uploadLegalPersonLicense");
            CountryPlatformResponse platformResponse = HanDanHttpSelectUtils.sendUploadLicense(HanDanHttpUtils.apiUrl + "/api/creditInquiry/uploadLegalPersonLicense", dto, token);
            log.info("授权完成" + JSONObject.toJSONString(platformResponse));
            if (platformResponse.isApiStatus()) {
                log.info("社保保险费缴纳情况查询完成");
                updateList.add(dto.getId());
            }
        });
    }
}


/* Location:              D:\tuodayun\workspace\work\celoan-third-data-report-service-2.0.0-SNAPSHOT.jar!\BOOT-INF\classes\com\celoan\service\statistics\impl\ExTradeEffectFinancialproductsDetailSqsServiceImpl.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */
